from typing import List, Any
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session

from ....api import deps
from ....schemas.counselor import CounselorResponse, CounselorUpdate
from ....models.user import User
from ....crud.counselor import counselor_crud

router = APIRouter()

@router.get("/", response_model=List[CounselorResponse])
def read_counselors(
        skip: int = 0,
        limit: int = 100,
        db: Session = Depends(deps.get_db),
        current_user: User = Depends(deps.get_current_user)
) -> Any:
    """
    获取所有辅导员列表
    """
    counselors = counselor_crud.get_multi(db, skip=skip, limit=limit)
    return counselors

@router.get("/{counselor_id}", response_model=CounselorResponse)
def read_counselor(
        counselor_id: int,
        db: Session = Depends(deps.get_db),
        current_user: User = Depends(deps.get_current_user)
) -> Any:
    """
    通过ID获取辅导员信息
    """
    counselor = counselor_crud.get(db, id=counselor_id)
    if not counselor:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="辅导员不存在"
        )
    return counselor

@router.put("/me", response_model=CounselorResponse)
def update_counselor_me(
        counselor_in: CounselorUpdate,
        db: Session = Depends(deps.get_db),
        current_user: User = Depends(deps.get_current_user)
) -> Any:
    """
    更新当前登录辅导员信息
    """
    if current_user.role != "counselor":
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="只有辅导员可以更新辅导员信息"
        )

    counselor = counselor_crud.get_by_user_id(db, user_id=current_user.id)
    if not counselor:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="辅导员信息不存在"
        )

    counselor = counselor_crud.update(db, db_obj=counselor, obj_in=counselor_in)
    return counselor